package com.cgtong.android.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cgtong.android.R;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.dyhdyh.widget.loading.factory.MaterialDialogFactory;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 懒加载基类
 */
public abstract class BaseLazyFragment extends Fragment {

    private boolean isVisible = false;
    private boolean isInitView = false;
    private boolean isFirstLoad = true;

    public abstract int getLayoutId();

    public abstract void initData();

    protected abstract void loadDataByNet();//网络请求处理

    protected abstract void onDataReloadByNet(View v);//再次请求处理

    private Unbinder mUnbinder;
    protected LoadService loadService;
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isVisible = true;
            lazyLoad();
        } else {
            isVisible = false;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayoutId(), container, false);
        isInitView = true;
        loadService = LoadSir.getDefault().register(rootView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                onDataReloadByNet(v);
            }
        });
        mUnbinder = ButterKnife.bind(this, rootView);
        lazyLoad();
        return loadService.getLoadLayout();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
    /**
     * 懒加载
     */
    private void lazyLoad() {
        if (!isFirstLoad || !isVisible || !isInitView) {
            //不加载数据
            return;
        }
        initData();
        //加载数据
        loadDataByNet();
        isFirstLoad = false;

    }

    /**
     * 显示加载框
     */
    public void doLoadingBar(Context context, boolean cancelable) {
        this.doLoadingBar(context, null, cancelable);
    }

    public void doLoadingBar(Context context, String msg, boolean cancelable) {
        LoadingDialog loadingDialog = LoadingDialog.make(context, new MaterialDialogFactory());
        if (!TextUtils.isEmpty(msg)) {
            loadingDialog.setMessage(msg);//提示消息
        } else {
            loadingDialog.setMessage(getString(R.string.request_net))//提示消息
                    .setCancelable(cancelable)
                    .create().show();
        }

    }

    public void doCancelLoadingBar() {
        LoadingDialog.cancel();
    }
    //loading end=====================================

}
